package com.yixun.qfbao.api.service.impl;

import com.alibaba.fastjson.JSON;
import com.yixun.qfbao.api.service.WechatTemplateMessageService;
import com.yixun.qfbao.constant.WxConstant;
import com.yixun.qfbao.util.HttpApi;
import com.yixun.qfbao.util.TemplateMessageResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class WechatTemplateMessageServiceImpl implements WechatTemplateMessageService {

    private static final Logger logger = LoggerFactory.getLogger(WechatTemplateMessageServiceImpl.class);

    @Override
    public TemplateMessageResult sendTemplate(String accessToken, String data) {
        logger.info("开始发送微信模板消息：发送数据="+data);
        String url = WxConstant.SEND_TEMPLATE_MESSAGE_URL.replace("ACCESS_TOKEN", accessToken);

        String result = HttpApi.doPost(url, data);
        logger.info("接收微信模板消息返回结果：接收数据="+result);

        //accessToken 报 40001 错误(invalid credential, access_token is invalid or not latest hint: [_BepEA09352352!]) 补偿机制
        TemplateMessageResult templateMessageResult = JSON.parseObject(result, TemplateMessageResult.class);
        return templateMessageResult;
    }
}
